import { patch } from "./vnode/patch";
import watcher from './observe/watcher'

export function mountComponent(vm, el) {
  callHook(vm, 'beforeMount');
  // vm._update(vm._render());
  let updateComponent = () => {
    vm._update(vm._render());
  }
  new watcher(vm, updateComponent, () => {
    callHook(vm, 'updated');
  }, true)

  callHook(vm, 'mounted');
}

/**
 * 生命周期
 * @param {*} Vue 
 */
export function lifecyeleMixin(Vue) {
  Vue.prototype._update = function (vnode) {
    let vm = this;
    vm.$el = patch(vm.$el, vnode)
  }
}

// 生命周期调用
export function callHook(vm, hook) {
  const handlers = vm.$options[hook];
  if (handlers) {
    for (let i = 0; i < handlers.length; i++) {
      handlers[i].call(vm);
    }
  }
}